package zhibi.cms.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.cms.domain.User;
import zhibi.cms.extra.base.AdminBaseController;
import zhibi.cms.extra.context.Constant;
import zhibi.cms.mapper.UserMapper;
import zhibi.commons.exception.MessageException;
import zhibi.utils.security.SecurityUtil;

@Controller
@RequestMapping(Constant.SYS_PREFIX + "/admin")
public class CmsAdminController extends AdminBaseController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("index")
    public String index() {
        return Constant.SYS_PREFIX + "/index";
    }

    @RequestMapping("welcome")
    public String welcome() {
        return Constant.SYS_PREFIX + "/welcome";
    }

    @GetMapping("login")
    public String login() {
        return Constant.SYS_PREFIX + "/login";
    }

    @PostMapping("login")
    public String login(String username, String password) {
        User user1 = new User();
        user1.setUsername(username);
        user1.setPassword(SecurityUtil.MD5(password).toUpperCase());
        user1 = userMapper.selectOne(user1);
        if (null == user1) throw new MessageException("用户名或密码错误");
        logger.info("【 登 录 】 用户:{}  ", user1.getUsername());
        session.setAttribute(Constant.SESSION.ADMIN, user1);
        return redirect("index");
    }


    @RequestMapping("logout")
    public String logout() {
        session.removeAttribute(Constant.SESSION.ADMIN);
        return redirect("index");
    }

}
